package cn.yuanqiao.activiti.listener;
/**
 * @author 赵常飞
 * @date 2023/9/28 10:53
 */

import cn.yuanqiao.activiti.service.IActArchiveDataService;
import cn.yuanqiao.common.exception.activiti.ArchiveProcessException;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 *
 *@AUTHOR:赵常飞
 *@date 2023/9/28 10:53
 */
@Component("YArchiveSaveComplatetListener")
public class YArchiveSaveComplatetListener implements ExecutionListener {
    @Autowired
    private IActArchiveDataService actArchiveDataService;

    @Override
    public void notify(DelegateExecution execution) {
        String processInstanceId = execution.getProcessInstanceId();
        Object obj = execution.getVariable("archiveData");

        if (obj == null){
            throw new ArchiveProcessException("档案数据不能为空");
        }
        List<Map<String,Object>> archiveData = (List<Map<String, Object>>) obj;
        Iterator<Map<String, Object>> iterator = archiveData.iterator();
        List<Map<String,Object>> archiveDataNotAgree = new ArrayList<>();
        while (iterator.hasNext()){
            Map<String, Object> archiveVariable = iterator.next();
            Object archiveAgreeStatus = archiveVariable.get("archiveAgreeStatus");
            if (archiveAgreeStatus!=null){
                String status = String.valueOf(archiveAgreeStatus);
                if ("0".equals(status)){
                    archiveDataNotAgree.add(archiveVariable);
                    iterator.remove();
                }
            }
        }
        if (archiveData.size()!=0){
            actArchiveDataService.batchUpdateArchiveState(archiveData,"10");
            actArchiveDataService.batchUpdateArchiveColumn(archiveData,"JS_TYPE","10");
        }
        if (archiveDataNotAgree.size()!=0){
            actArchiveDataService.batchUpdateArchiveState(archiveDataNotAgree,"2");
        }
        System.out.println("同意预归档，修改数据");

    }
}
